//
//  YCPriceManager.m
//  Pods
//
//  Created by Apple on 2022/11/3.
//

#import "YCPriceManager.h"

static NSString * const YCCentUnit = @"100";

@implementation YCPriceManager

+ (NSString *)priceToIntegerOrDecimal:(long)price {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    NSString *string = [formatter stringFromNumber:@(price/100.0)];
    return string;
}

/// 15000 = @“150.00”
/// @param price price description
+ (NSString *)priceToString:(long)price {
    NSString *priceString = [NSString stringWithFormat:@"%ld", price];
    NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:priceString];
    NSDecimalNumber *divideNumber = [NSDecimalNumber decimalNumberWithString:YCCentUnit];
    NSDecimalNumber *resultNumber = [priceNumber decimalNumberByDividingBy:divideNumber];
    NSString *money = [NSString stringWithFormat:@"%.2f", resultNumber.doubleValue];
    return money;
}

+(NSString *)priceToStrWithSeparator:(NSInteger)price
{
    NSString *priceString = [NSString stringWithFormat:@"%.2f", price/100.0];
    
    NSNumberFormatter *dataFormatter = [[NSNumberFormatter alloc] init];
    dataFormatter.numberStyle = NSNumberFormatterDecimalStyle;
    dataFormatter.maximumFractionDigits = 2;
    dataFormatter.minimumFractionDigits = 2;
    
    NSNumber *num = [dataFormatter numberFromString:priceString];
    NSString *tempStr = [dataFormatter stringFromNumber:num];
    
    return tempStr;
}

/// 15000 = @“150.00 元”
/// @param price price description
+ (NSString *)priceToUnitString:(long)price {
    NSString *priceString = [NSString stringWithFormat:@"%ld", price];
    NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:priceString];
    NSDecimalNumber *divideNumber = [NSDecimalNumber decimalNumberWithString:YCCentUnit];
    NSDecimalNumber *resultNumber = [priceNumber decimalNumberByDividingBy:divideNumber];
    NSString *money = [NSString stringWithFormat:@"%.2f 元", resultNumber.doubleValue];
    return money;
}

/**
 *  金额 -> 文本(按分进行转换) 15000 = ￥150.00
 */
+ (NSString *)priceToFlagString:(long)price {
    NSString *priceString = [NSString stringWithFormat:@"%ld", price];
    NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:priceString];
    NSDecimalNumber *divideNumber = [NSDecimalNumber decimalNumberWithString:YCCentUnit];
    NSDecimalNumber *resultNumber = [priceNumber decimalNumberByDividingBy:divideNumber];
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    formatter.positiveFormat = @"###,##0.00";
    NSString *amountString = [formatter stringFromNumber:resultNumber];
    amountString = [NSString stringWithFormat:@"￥ %@",amountString];
    return amountString;
}

+ (NSString *)intPriceString:(long)price {
    NSString *priceString = [NSString stringWithFormat:@"%ld", price];
    NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:priceString];
    NSDecimalNumber *divideNumber = [NSDecimalNumber decimalNumberWithString:YCCentUnit];
    NSDecimalNumber *resultNumber = [priceNumber decimalNumberByDividingBy:divideNumber];
    
    NSString *money = [NSString stringWithFormat:@"%.0f", resultNumber.doubleValue];
    return money;
}


+ (NSInteger)priceNumberFromPriceString:(NSString *)priceString {
    NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:priceString];
    NSDecimalNumber *multiplyNuber = [NSDecimalNumber decimalNumberWithString:YCCentUnit];
    NSDecimalNumber *resultNumber = [priceNumber decimalNumberByMultiplyingBy:multiplyNuber];
    return [resultNumber integerValue];
}

+ (NSString *)tenThousandPriceString:(long)price {
    NSString *priceString = [NSString stringWithFormat:@"%ld", price];
    NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:priceString];
    NSDecimalNumber *divideNumber = [NSDecimalNumber decimalNumberWithString:YCCentUnit];
    NSDecimalNumber *resultNumber = [priceNumber decimalNumberByDividingBy:divideNumber];
    
    NSString *money = [NSString stringWithFormat:@"%.2f", resultNumber.doubleValue/10000];
    return money;
}


+ (NSString *)tenThousandIntegerPriceString:(long)price {
    NSString *priceString = [NSString stringWithFormat:@"%ld", price];
    NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:priceString];
    NSDecimalNumber *divideNumber = [NSDecimalNumber decimalNumberWithString:YCCentUnit];
    NSDecimalNumber *resultNumber = [priceNumber decimalNumberByDividingBy:divideNumber];
    
    NSString *money = [NSString stringWithFormat:@"%.2f", resultNumber.doubleValue/10000.0];
    return money;
}

+ (NSString *)priceStringWithPriceNumber:(NSInteger)number numberUnit:(NSInteger)unit {
    
    CGFloat price = number/100.f/unit;
    CGFloat max = ceil(price);
    NSString *formatStr = max == price ? @"###0" : @"###0.00";
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setPositiveFormat:formatStr];
    formatter.multiplier = @(1.f/unit);
    formatter.roundingMode = NSNumberFormatterRoundFloor;
    formatter.maximumFractionDigits = 2;
    return [formatter stringFromNumber:@(number/100.f)];
}

+ (NSString *)priceFormatWithPrice:(NSInteger)pirce {
    // 判断是否null 若是赋值为0 防止崩溃
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    // 注意传入参数的数据长度，可用double
    NSString *money = [formatter stringFromNumber:[NSNumber numberWithInteger:pirce]];
    return money;
}

+ (NSString *)priceToFittedString:(NSInteger)price {
    NSString *string = @"0.00";
    if (price >= 1000000) {
        string = [self tenThousandPriceString:price];
    }
    else {
        string = [self intPriceString:price];
    }
    return string;
}

+ (NSString *)priceToFittedUnitString:(NSInteger)price {
    NSString *string = @"0.00";
    if (price >= 1000000) {
        string = [self tenThousandPriceString:price];
        string = [NSString stringWithFormat:@"%@万", string];
    }
    else {
        string = [self intPriceString:price];
        string = [NSString stringWithFormat:@"%@元", string];
    }
    return string;
}

+ (void)formatNumberStrWithValue:(NSInteger)value unit:(NSString *)unit complete:(void(^)(NSString *value, NSString *unit))complete {
    if (value >= 100000000) {
        unit = [NSString stringWithFormat:@"亿%@", unit]; //kStringFormat(@"亿%@", unit);
        value = value / 100000000;
    } else if (value >= 10000) {
        unit = [NSString stringWithFormat:@"万%@", unit];
        value = value / 10000;
    }
    
    NSString *str = [NSString stringWithFormat:@"%ld", value];
    
    complete(str, unit);
}

@end
